package lab;

import process.Actor;
import process.DispatcherFinishException;
import process.IWaitCondition;
import process.Store;
import rnd.Randomable;

public class Ship extends Actor {
	
	private Store container;
	private int maxContainers;
	private double finishTime;
	private Randomable rnd;
	
	public Ship(String name, mainGUI gui, Model model){
		setNameForProtocol(name);
		container=model.getqContainer();
		maxContainers = gui.getCountOfShips().getInt();
		finishTime = gui.getTimeWork().getDouble();
		rnd = gui.getTimeRandom();
	}

	@Override
	protected void rule() {
		// TODO Auto-generated method stub
		
			IWaitCondition cc = new IWaitCondition() {
				public boolean testCondition() {
					return container.getSize()+10<=maxContainers;
				}
				public String toString() {
					return ("port zapolnen");
				}
			};
			while (true) {
				holdForTime(rnd.next());
				if (getDispatcher().getCurrentTime() > finishTime)
					break;

				getDispatcher().printToProtocol(
						"  " + getNameForProtocol() + "konteyneru prupluli");
				container.add(10);
				if (container.getSize()+10 >= maxContainers) {
					try {
						waitForCondition(cc);
					} catch (DispatcherFinishException e) {
						return;
					}
				}
			
			}
		}


}
